package com.example.cheeseerpadmin.bus.service.impl;

import com.example.cheeseerpadmin.bus.entity.BusGoods;
import com.example.cheeseerpadmin.bus.entity.BusInport;
import com.example.cheeseerpadmin.bus.entity.BusSales;
import com.example.cheeseerpadmin.bus.mapper.BusGoodsMapper;
import com.example.cheeseerpadmin.bus.mapper.BusSalesMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cheeseerpadmin.bus.service.BusSalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cheese
 * @since 2022-02-18
 */
@Service
@Transactional
public class BusSalesServiceImpl extends ServiceImpl<BusSalesMapper, BusSales> implements BusSalesService {

    @Autowired
    public BusGoodsMapper busGoodsMapper;

    @Override
    public boolean save(BusSales entity) {
        BusGoods busGoods = busGoodsMapper.selectById(entity.getGoodsid());
        busGoods.setNumber(busGoods.getNumber() - entity.getNumber());
        busGoodsMapper.updateById(busGoods);
        return super.save(entity);
    }

    @Override
    public boolean updateById(BusSales entity) {
        BusGoods busGoods = busGoodsMapper.selectById(entity.getGoodsid());
        BusSales busSales = this.getBaseMapper().selectById(entity.getId());
        busGoods.setNumber(busGoods.getNumber() - entity.getNumber() + busSales.getNumber());
        busGoodsMapper.updateById(busGoods);
        return super.updateById(entity);
    }

    @Override
    public boolean removeById(Serializable id) {
        BusSales busSales = this.getBaseMapper().selectById(id);
        BusGoods busGoods = busGoodsMapper.selectById(busSales.getGoodsid());
        busGoods.setNumber(busGoods.getNumber() + busSales.getNumber());
        busGoodsMapper.updateById(busGoods);
        return super.removeById(id);
    }
}
